package com.dormitory.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.dormitory.R;
import com.dormitory.application.MyApplication;
import com.dormitory.bean.UserBean;
import com.dormitory.controller.UserController;

public class LoginActivity extends AppCompatActivity {

    SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        sp = getApplicationContext().getSharedPreferences("login", MODE_PRIVATE);
        ((TextView)findViewById(R.id.editUsername)).setText(sp.getString("name", ""));
        findViewById(R.id.text_register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
        findViewById(R.id.forget_password).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, ForgetActivity.class);
                startActivity(intent);
            }
        });
        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = ((TextView) findViewById(R.id.editUsername)).getText().toString();
                String password = ((TextView)findViewById(R.id.editPassword)).getText().toString();
                LoginAsyncTask loginAsyncTask = new LoginAsyncTask();
                loginAsyncTask.execute(name, password);

                /**/

                /*Intent intent = new Intent(LoginActivity.this, FilePassActivity.class);
                startActivity(intent);
                finish();*/
            }
        });
    }
    private class LoginAsyncTask extends AsyncTask<String, Object, Integer> {

        private UserBean user;
        @Override
        protected Integer doInBackground(String[] params) {
            user = new UserBean();
            UserController userController = UserController.getInstance();
            return userController.checkUser(params[0], params[1], user);
        }

        @Override
        protected void onPostExecute(Integer code) {
            super.onPostExecute(code);
            switch(code) {
                case 0:
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putBoolean("autoLogin", ((CheckBox)findViewById(R.id.check_auto_login)).isChecked());
                    editor.putString("name", ((TextView) findViewById(R.id.editUsername)).getText().toString());
                    editor.putString("password", ((TextView)findViewById(R.id.editPassword)).getText().toString());
                    editor.commit();
                    ((MyApplication)getApplication()).setUser(user);
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                    break;
                case 1:
                    Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    Toast.makeText(LoginActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
